wxapp.logic.networkCenter = {
    isTimerInited: !1,
    heartTickCount: 0,
    heartCount: 0,
    errorCount: 0,
    _stopNetwork: !1,
    _socket: null,
    _login: null,
    boot: function() {
        this._socket = wxapp.logic.socket, this._login = wxapp.logic.networkLogin, tywx.NotificationCenter.listen(tywx.EventType.SDK_LOGIN_SUCCESS, this.onTyLoginSuccess, this), 
        tywx.NotificationCenter.listen(tywx.EventType.SDK_LOGIN_FAIL, this.onTyLoginFail, this), 
        tywx.NotificationCenter.listen(tywx.EventType.WEIXIN_LOGIN_SUCCESS, this.onWxLoginSuccess, this), 
        tywx.NotificationCenter.listen(tywx.EventType.WEIXIN_LOGIN_FAIL, this.onWxLoginFail, this), 
        tywx.NotificationCenter.listen(tywx.EventType.SEND_HEART_BEAT, this.onHeartBeat, this), 
        tywx.NotificationCenter.listen(tywx.EventType.TCP_OPENED, this.onTcpOpen, this), 
        tywx.NotificationCenter.listen(tywx.EventType.TCP_ERROR, this.onTcpError, this), 
        tywx.NotificationCenter.listen(tywx.EventType.TCP_CLOSE, this.onTcpClose, this), 
        tywx.NotificationCenter.listen(tywx.EventType.TCP_RECONNECT, this.onTcpReconnect, this), 
        tywx.NotificationCenter.listen(tywx.EventType.TCP_RECEIVE, this.onTcpReceive, this), 
        tywx.NotificationCenter.listen(tywx.EventType.GAME_SHOW, this.onGameShow, this), 
        tywx.NotificationCenter.listen(tywx.EventType.GAME_HIDE, this.onGameHide, this), 
        tywx.NotificationCenter.listen(tywx.EventType.EVT_DEBUG_STOP_NETWORK, this.onDebugStopNetwork, this), 
        tywx.NotificationCenter.listen(tywx.EventType.EVT_NET_CLOSE, this.onNetClose, this), 
        this.initMonitorNetworkStatus();
    },
    initMonitorNetworkStatus: function() {
        var t = function(t) {
            t.hasOwnProperty("isConnected") ? tywx.StateInfo.networkConnected = t.isConnected : t.hasOwnProperty("errMsg") ? tywx.StateInfo.networkConnected = "getNetworkType:ok" == t.errMsg : tywx.StateInfo.networkConnected = "none" != t.networkType, 
            tywx.StateInfo.networkType = t.networkType;
            var e = tywx.StateInfo.networkStatus;
            if ("none" == t.networkType ? tywx.StateInfo.networkStatus = 0 : tywx.StateInfo.networkStatus = 1, 
            tywx.StateInfo.networkStatus != e) {
                tywx.NotificationCenter.trigger(tywx.EventType.NETWORK_STATUS_CHANGED, tywx.StateInfo.networkStatus);
                0 == tywx.StateInfo.networkStatus && "未连接网络";
            }
        };
        wx.getNetworkType({
            success: t
        }), wx.onNetworkStatusChange(t);
    },
    login: function() {
        this._stopNetwork || (wxapp.netGuest ? wxapp.logic.networkLogin.loginGuest() : wxapp.logic.networkLogin.login());
    },
    onWxLoginFail: function() {
        var t = this;
        wxapp.appData.firstLoadSuccess, t.scheduleOnce(function() {
            t.login();
        }, 1);
    },
    onWxLoginSuccess: function() {},
    onTyLoginFail: function() {
        var t = this;
        wxapp.appData.firstLoadSuccess, t.scheduleOnce(function() {
            t.login();
        }, 1);
    },
    onTyLoginSuccess: function() {
        wxapp.logic.socket.connect(tywx.SystemInfo.webSocketUrl), wxapp.model.adMgr.init();
    },
    onHeartBeat: function() {
        this.heartCount++, wxapp.twarn("heart", "send heart", this.heartCount), wxapp.SendMsg.sendHeartBeat();
    },
    onTcpOpen: function() {
        this.errorCount = 0, this.heartCount = 0, this.startSocketTimer();
        var t = wxapp.model.sceneInfo.getCacheParam(), e = 0;
        null != t && t.query && t.query.inviteCode && (e = t.query.inviteCode), tywx.NotificationCenter.trigger(tywx.EventType.EVT_APP_TCP_OPEN, e);
    },
    onTcpError: function() {
        this.errorCount++, tywx.NotificationCenter.trigger(tywx.EventType.EVT_APP_TCP_ERROR), 
        this.startSocketTimer();
    },
    onTcpClose: function() {
        tywx.NotificationCenter.trigger(tywx.EventType.EVT_APP_TCP_CLOSE), ty && ty.NotificationCenter && ty.NotificationCenter.trigger(ty.EventType.TCP_CLOSE);
    },
    onTcpReconnect: function() {
        ty && ty.NotificationCenter && ty.NotificationCenter.trigger(ty.EventType.TCP_RECONNECT);
    },
    onTcpReceive: function(t) {
        var e = t.cmd, n = t.result;
        if (n) {
            n.gameId;
            wxapp.logic.socket.filterCmds.indexOf(e), e == tywx.EventType.MSG_USER_INFO ? tywx.NotificationCenter.trigger(tywx.EventType.EVT_USER_INFO, t) : ty.netMsgDispatcher.processMsg(t.cmd, [ t ]);
        }
    },
    onGameShow: function(t) {
        wxapp.model.sceneInfo.getUiState() == wxapp.model.sceneInfo.UI_STATE.UI_STATE_GAME && this.login();
    },
    onGameHide: function() {},
    socketTimerSchedule: function() {
        this.heartTickCount = (this.heartTickCount + 1) % 3, 2 == this.heartTickCount && wxapp.logic.socket.connectStatus == wxapp.logic.socket.CONNECT_STATUS_OK && tywx.NotificationCenter.trigger(tywx.EventType.SEND_HEART_BEAT), 
        wxapp.logic.socket.reconnet();
    },
    startSocketTimer: function() {
        this.isTimerInited || (this.timerFunc = this.socketTimerSchedule.bind(this), this.isTimerInited = !0, 
        tywx.Timer.setTimer(cc.director, this.timerFunc, 1));
    },
    stopSocketTimer: function() {
        this.isTimerInited && (this.isTimerInited = !1, tywx.Timer.cancelTimer(cc.director, this.timerFunc));
    },
    showModal: function(t) {
        wx.showModal({
            title: "提示",
            content: "网络未连接, 请检查网络",
            showCancel: !1,
            success: function(e) {
                e.confirm ? t() : e.cancel;
            }
        });
    },
    scheduleOnce: function(t, e) {
        e || (e = 1), setTimeout(t, 1e3 * e);
    },
    scheduleOnceShowModal: function(t, e) {
        var n = this;
        this.scheduleOnce(function() {
            n.showModal(t);
        }, e);
    },
    onDebugStopNetwork: function() {
        this._stopNetwork = !0, this.stopSocketTimer(), this._socket.close();
    },
    onNetClose: function() {
        this._socket.close();
    }
};